package com.imooc.servlet.session;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * 
 * 测试Session-用户会话，用户指的是浏览器
 * 获取session用户会话对象，用getAttribute获取session对象的属性值
 *
 */
@WebServlet("/session/index")
public class SessionIndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
 
    public SessionIndexServlet() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取session用户会话对象
		HttpSession session =  request.getSession();
		//获取session对象存储空间的id，然后输出
		String sessionId = session.getId();
		System.out.println(sessionId);
		//getAttribute获取session对象的属性值
		String name = (String) session.getAttribute("name");
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().println("用户是： " + name);
	}

}
